package football.haha.com.footballtv.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import football.haha.com.footballtv.FootballApplication;
import football.haha.com.footballtv.R;
import football.haha.com.footballtv.bean.BeanHighLights;
import football.haha.com.footballtv.bean.BeanMutiContent;
import football.haha.com.footballtv.bean.BeanMutiContentInfo;
import football.haha.com.footballtv.bean.BeanOrder;
import football.haha.com.footballtv.bean.BeanPlayRecord;
import football.haha.com.footballtv.bean.BeanSchedule;
import football.haha.com.footballtv.bean.BeanUCSResp;
import football.haha.com.footballtv.utils.ApiUtils;
import football.haha.com.footballtv.utils.ImageToos;
import football.haha.com.footballtv.utils.NetUtils;
import football.haha.com.footballtv.utils.Tools;
import football.haha.com.footballtv.view.SmoothVorizontalScrollView;

/**
 * Created by haha on 2017/10/7.
 */

public class MyRecordActivity extends BaseActivity {


    @Bind(R.id.cb_record_play)
    CheckBox cbRecordPlay;
    @Bind(R.id.cb_record_order)
    CheckBox cbRecordOrder;
    @Bind(R.id.fl_record_content)
    FrameLayout flRecordContent;
    @Bind(R.id.svsv_record)
    SmoothVorizontalScrollView svsvRecord;
    @Bind(R.id.fl_main)
    FrameLayout flMain;

    private BeanMutiContentInfo orderMutiContentInfo;
    private BeanMutiContentInfo playMutiContentInfo;
    public ArrayList<View> scheduleContentItemList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_record);
        ButterKnife.bind(this);

        createOnlyBorderView(flMain);
        borderView.setBorderBitmapResId(R.drawable.focus_tab, (int) getResources().getDimension(R.dimen.px100));
        cbRecordPlay.setOnFocusChangeListener(this);
        cbRecordOrder.setOnFocusChangeListener(this);
        scale = 1.0f;

    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            switch (v.getId()) {
                case R.id.cb_record_play:
                    cbRecordPlay.setChecked(true);
                    cbRecordOrder.setChecked(false);
                    borderView.setBorderBitmapResId(R.drawable.focus_tab, (int) getResources().getDimension(R.dimen.px100));
                    if (playMutiContentInfo!=null){
                        addRecordList(playMutiContentInfo.getContents());
                    }else{
                        getRecord("1"); //播放历史
                    }

                    break;
                case R.id.cb_record_order:
                    cbRecordOrder.setChecked(true);
                    cbRecordPlay.setChecked(false);
                    borderView.setBorderBitmapResId(R.drawable.focus_tab, (int) getResources().getDimension(R.dimen.px100));
                    if (orderMutiContentInfo!=null){
                        addRecordList(orderMutiContentInfo.getContents());
                    }else{
                        getRecord("3"); //预约查询
                    }
                    break;
                default:
                    borderView.setBorderBitmapResId(R.drawable.focus_length, (int) getResources().getDimension(R.dimen.px180),
                            (int) getResources().getDimension(R.dimen.px196));
                    break;
            }
        }
        super.onFocusChange(v, hasFocus);
    }

    public void addRecordList(List<BeanMutiContentInfo.ContentsBean> contentsBeanList) {
        scheduleContentItemList.clear();
        flRecordContent.removeAllViews();
        int allHeight = 0;
        for (int i = 0; i < contentsBeanList.size(); i++) {
            View itemSchedule = View.inflate(this, R.layout.item_schedule_football, null);
            itemSchedule.setOnFocusChangeListener(this);
            itemSchedule.setOnClickListener(this);
            itemSchedule.setId(i + 2000);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    (int) getResources().getDimension(R.dimen.px140));

            int topMargin = i * (int) getResources().getDimension(R.dimen.px150);
            int leftMargin = (int) getResources().getDimension(R.dimen.px320);

            params.topMargin = topMargin;
            params.leftMargin = leftMargin;
            params.rightMargin = (int) getResources().getDimension(R.dimen.px120);

            flRecordContent.addView(itemSchedule, params);
            allHeight = topMargin + params.height + (int) getResources().getDimension(R.dimen.px150);

            if (i == 0) {
                itemSchedule.setNextFocusUpId(R.id.cb_schedule);
            }
            scheduleContentItemList.add(itemSchedule);


            BeanMutiContentInfo.ContentsBean contentsBean = contentsBeanList.get(i);
            TextView tvName = (TextView) itemSchedule.findViewById(R.id.tv_name);
            tvName.setText(contentsBean.getName());
            TextView tvState = (TextView) itemSchedule.findViewById(R.id.tv_state);

            TextView tvOrderState = (TextView) itemSchedule.findViewById(R.id.tv_order_state);

            if (TextUtils.equals("1", contentsBean.getProgramType())) {
                tvState.setText("正在直播");
                tvState.setTextColor(0xff48C629);
                tvOrderState.setText("");
            } else if (TextUtils.equals("2", contentsBean.getProgramType())) {
                tvState.setText("已结束");
                tvState.setTextColor(0xff268FFB);
                tvOrderState.setText("");
            } else {
                tvState.setText(Tools.getSchedueStartTime(contentsBean.getStartTime()));
                tvState.setTextColor(0xff8D93A2);
            }

            if (Tools.isOrder(this, contentsBean.getContentId())) {
                tvOrderState.setText("已预约");
            } else {
                tvOrderState.setText("");
            }


            TextView tvResult = (TextView) itemSchedule.findViewById(R.id.tv_result);
            tvResult.setText(contentsBean.getScore());

            TextView tvLeftTeamName = (TextView) itemSchedule.findViewById(R.id.tv_left_team_name);
            tvLeftTeamName.setText(contentsBean.getHomeTeamName());

            TextView tvRightTeamName = (TextView) itemSchedule.findViewById(R.id.tv_right_team_name);
            tvRightTeamName.setText(contentsBean.getAwayTeamName());

            ImageView ivLeftIcon = (ImageView) itemSchedule.findViewById(R.id.iv_left_team_icon);
            ImageView ivRightIcon = (ImageView) itemSchedule.findViewById(R.id.iv_right_team_icon);

            ImageToos.setImageWithUrl(contentsBean.getHomeTeamLogo(), ivLeftIcon, FootballApplication.getOptions(R.drawable.default_team));
            ImageToos.setImageWithUrl(contentsBean.getAwayTeamLogo(), ivRightIcon, FootballApplication.getOptions(R.drawable.default_team));

        }

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, allHeight);
        flRecordContent.setLayoutParams(params);
    }


    private void getRecord(String type) {
        Map<String, String> params = new HashMap<>();
        params.put("requestType", type);
        params.put("command", "2");
        params.put("userId", Tools.getDeviceId(this));
        params.put("isTop", "1");
        params.put("topNum", "20");

        if (TextUtils.equals("1", type)) { //播放历史
            NetUtils.postJsonData(this, ApiUtils.UCS_URL, ApiUtils.TAGGetHistoryData, params, BeanPlayRecord.class, "加载中...", this);
        } else if (TextUtils.equals("3", type)) { //预约记录
            NetUtils.postJsonData(this, ApiUtils.UCS_URL, ApiUtils.TAGGetOrderData, params, BeanOrder.class, "加载中...", this);
        }


    }

    @Override
    public void netBack(int requestTag, Object object) {
        super.netBack(requestTag, object);
        if (requestTag == ApiUtils.TAGGetHistoryData) {
            BeanPlayRecord beanPlayRecord = (BeanPlayRecord) object;
            List<BeanPlayRecord.ContentListBean> contentList = beanPlayRecord.getContentList();

            BeanMutiContent mutiContent = new BeanMutiContent();
            List<BeanMutiContent.ContentsBean> contents = new ArrayList<>();
            for (int i=0; i<contentList.size(); i++){
                String contentId = contentList.get(i).getContentId();

                BeanMutiContent.ContentsBean contentsBean = new BeanMutiContent.ContentsBean();
                contentsBean.setContentId(contentId);
                contentsBean.setFolderCode("");

                contents.add(contentsBean);
            }
            mutiContent.setContents(contents);

            String contentStr = JSON.toJSONString(mutiContent);
            NetUtils.postStringData(this, beanPlayRecord.getUrl()+"?command=contentList&&busCode=IPS", ApiUtils.TAGGetPlayRecordInfoData,
                    contentStr, BeanMutiContentInfo.class,  null, this);

        }else if (requestTag == ApiUtils.TAGGetOrderData) {
            BeanOrder beanOrder = (BeanOrder) object;

            BeanMutiContent mutiContent = new BeanMutiContent();
            List<BeanMutiContent.ContentsBean> contents = new ArrayList<>();
            for (int i=0; i<beanOrder.getScheduleList().size(); i++){
                String contentId = beanOrder.getScheduleList().get(i);

                BeanMutiContent.ContentsBean contentsBean = new BeanMutiContent.ContentsBean();
                contentsBean.setContentId(contentId);
                contentsBean.setFolderCode("");

                contents.add(contentsBean);
            }
            mutiContent.setContents(contents);

            String contentStr = JSON.toJSONString(mutiContent);
            NetUtils.postStringData(this, beanOrder.getUrl()+"?command=contentList&&busCode=IPS", ApiUtils.TAGGetOrderRecordInfoData,
                    contentStr, BeanMutiContentInfo.class,  null, this);
        }else if (requestTag == ApiUtils.TAGGetOrderRecordInfoData){
            orderMutiContentInfo = (BeanMutiContentInfo) object;
            addRecordList(orderMutiContentInfo.getContents());
        }else if (requestTag == ApiUtils.TAGGetPlayRecordInfoData){
            playMutiContentInfo = (BeanMutiContentInfo) object;
            addRecordList(playMutiContentInfo.getContents());
        }
    }
}
